package com.kkd.kkdapp.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.MyDiscountCouponActivity;
import com.kkd.kkdapp.adapter.MyDiscountCouponAdapter;
import com.kkd.kkdapp.http.entity.MyDiscountCouponData;
import com.kkd.kkdapp.http.entity.MyDiscountCouponDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.utils.UIEmptyHandleUtil;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

/**
 * 已过期
 * 
 * @author mty
 * 
 */
public class MyDiscountCouponHaveExpiredFragment extends Fragment implements
		KKDScrollListener {

	private static final int CODLE_ZERO = 1;
	private static final int CODLE_ELSE = 2;

	private MyDiscountCouponActivity mMyDiscountCouponActivity;
	private ImageView mNullImg;
	private TextView mNullTv;
	private KkdXlistView manage_lv;
	// private String version;
	private int pageNO = 1;
	private int pageSize = 10;
	private List<MyDiscountCouponDataList> mList;
	private boolean REFRESHFLAG = true;
	private MyDiscountCouponAdapter adapter;
	private String couponType = "";
	private ListView mListView;
	Toast mToast;
	private LinearLayout ll_null;
	private boolean firstEntryFlag = true;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODLE_ZERO:
				MyDiscountCouponData mMyDiscountCouponData = (MyDiscountCouponData) msg.obj;
				int pageNO = mMyDiscountCouponData.getPageNO();
				int pageCount = mMyDiscountCouponData.getPageCount();
				if (pageNO > pageCount) {
					if (pageCount == 0) {
						mList.clear();
						adapter.notifyDataSetChanged();
					} else {
						manage_lv.stopLoadMore(-1);
					}
					if (REFRESHFLAG) {
						manage_lv.stopLoadMore(1);
					}
				} else {
					if (pageNO == 1 && mList != null) {
						mList.clear();
						manage_lv.setNoMore(false);
					}
					mList.addAll(mMyDiscountCouponData.getDataList());
					adapter.notifyDataSetChanged();
					if (mMyDiscountCouponData.getDataList().size() < pageSize) {
						manage_lv.stopLoadMore(1);
					} else {
						manage_lv.stopLoadMore(0);
					}
					Time_Date time_Date = new Time_Date();
					String time = time_Date.get_Year_Day();
					manage_lv.setRefreshTime(time);
				}
				manage_lv.stopRefresh();
				firstEntryFlag = true;
				if (REFRESHFLAG) {
					if (mList != null && mList.size() > 0) {
						firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
								mMyDiscountCouponActivity, firstEntryFlag,
								false, ll_null, mNullImg, mNullTv, "0", 0);
					} else {
						firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
								mMyDiscountCouponActivity, firstEntryFlag,
								true, ll_null, mNullImg, mNullTv, "没有已过期的优惠券",
								R.drawable.img_null);
					}
				}
				break;
			case CODLE_ELSE:
				int statusCode = msg.arg1;
				if (statusCode == KkdHttpClient.NO_NET) {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							mMyDiscountCouponActivity, firstEntryFlag, true,
							ll_null, mNullImg, mNullTv, "网络错误，请稍后重试",
							R.drawable.img_null_net);
				} else {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							mMyDiscountCouponActivity, firstEntryFlag, true,
							ll_null, mNullImg, mNullTv, "服务异常，请稍后重试",
							R.drawable.img_null_net);
				}
				manage_lv.stopLoadMore(1);
				manage_lv.stopRefresh();
				break;
			default:
				break;

			}
		};
	};

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mMyDiscountCouponActivity = (MyDiscountCouponActivity) activity;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View contentView = inflater.inflate(R.layout.mydiscountcoupon_fragment,
				null);
		manage_lv = (KkdXlistView) contentView
				.findViewById(R.id.deal_detail_list);
		if (mList == null) {
			mList = new ArrayList<MyDiscountCouponDataList>();
		}
		manage_lv.setKKDScrollListener(this);
		mListView = manage_lv.mListView;
		mListView.setDividerHeight(0);
		adapter = new MyDiscountCouponAdapter(getActivity(), false, mList, -3,
				"");
		mListView.setAdapter(adapter);
		manage_lv.startRefresh();

		mNullImg = (ImageView) contentView.findViewById(R.id.id_no_content);
		mNullTv = (TextView) contentView.findViewById(R.id.id_no_content_text);
		ll_null = (LinearLayout) contentView.findViewById(R.id.ll_null);
		couponList();
		return contentView;
	}

	public void updateMyDiscountCouponHaveExpiredFragment(String couponType) {
		this.couponType = couponType;
		couponList();

	}

	/**
	 * 优惠券
	 */
	public void couponList() {

		KkdHttpMagager.getInstance().getMyDiscountManager("", couponType, "",
				"", pageNO, pageSize, 2, MyDiscountCouponData.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						MyDiscountCouponData mMyDiscountCouponData = (MyDiscountCouponData) responseBody;
						Message msg = new Message();
						msg.obj = mMyDiscountCouponData;
						msg.what = CODLE_ZERO;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.arg1 = statusCode;
						msg.what = CODLE_ELSE;
						mHandler.sendMessage(msg);
					}
				});

	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		couponList();
		REFRESHFLAG = true;
	}

	@Override
	public void onLoadMore() {

		pageNO++;
		REFRESHFLAG = false;
		couponList();
	}

	public void showToast(String hintLanguage) {
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
		mToast = Toast.makeText(mMyDiscountCouponActivity, hintLanguage,
				Toast.LENGTH_SHORT);
		mToast.show();
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
	}
}
